//index.js
//获取应用实例
const app = getApp()
var jsonData = require('../../data/json.js');
Page({
  data: {
    selectArray: {
      text: '', 
      id: '',
      nodes:[{}]
    }
  },
  tapItem: function (e) {
    var itemid = e.detail.itemid;
    var itemval = e.detail.value;
    console.log("所选中的分区编号：" + itemid + "， 名称：" + itemval);
  },
  onLoad: function () {
    let dataList = jsonData.dataList;
    let categoryTree = []; 
    let list = dataList.reduce(function(prev, item){
                    prev[item.pId]?prev[item.pId].push(item):prev[item.pId] = [item];
                    return prev
                },{});        
    for (let key in list) {
        list[key].forEach(function (item) { 
        item.nodes = list[item.id] ? list[item.id] : [];
        });
    }
    categoryTree = list[0];
    this.setData({
      //jsonData.dataList获取json.js里定义的json数据，并赋值给dataList
      [`selectArray.nodes`]: categoryTree
    });
    console.info(this.data.selectArray);
  }
})

